#import "SEGMarketDetailView.h"


#import <UIImageView+WebCache.h>
#import "UIImageView+SEGWebCache.h"

#import <Masonry/Masonry.h>

#if __has_include(<Commom/SEGLayoutMacro.h>)
#import <Commom/SEGLayoutMacro.h>
#else
#import "SEGLayoutMacro.h"
#endif

#if __has_include(<Commom/SEGColorManager.h>)
#import <Commom/SEGColorManager.h>
#else
#import "SEGColorManager.h"
#endif


#if __has_include(<Commom/YYBaseHeader.h>)
#import <Commom/YYBaseHeader.h>
#else
#import "YYBaseHeader.h"
#endif

/**************************SEGMarketDetailView************************/
@implementation SEGMarketDetailView
- (instancetype)initWithFrame:(CGRect)frame {
    if (frame.size.width == 0 && frame.size.height == 0) {
        frame.size.width = [UIScreen mainScreen].bounds.size.width;
        frame.size.height = kMarketDetailViewHeight;
    }
    self = [super initWithFrame:frame];
    
    [self addSubview:self.titleLabel];
    [self addSubview:self.contentLabel];
    [self addSubview:self.priceLabel];
    
    [self addSubview:self.imageContentView];

     self.userInteractionEnabled = YES;
//     __weak typeof(self) _self = self;
//     UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithActionBlock:^(UITapGestureRecognizer*  _Nonnull sender) {
//     [SEGAlertView showTmepMessageWithMessage:@"SEGMarketDetailViewClick"];
//     }];
//     [self addGestureRecognizer:tap];
    
//    self.backgroundColor = kSegRandomColor;
    return self;
}

-(void)setWithLayout:(SEGMarketDetailViewLayout *)layout{
    self.height = layout.totalHeight;
    _layout = layout;
    
    if (self.height <= 0) return;
    
    self.titleLabel.textLayout = layout.titleLabelTextLayout;
    self.contentLabel.textLayout = layout.contentLabelTextLayout;
    self.priceLabel.textLayout = layout.priceLabelTextLayout;
    
    [self setupUI];
}

//界面布局
-(void)setupUI{
    if (_layout.data.isGift) {//赠送不显示价格
        self.priceLabel.hidden = YES;
        [self.titleLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.left.top.equalTo(self);
            make.right.equalTo(self);
            make.height.mas_equalTo(self.layout.titleLabelHeight);
        }];
    }else {
        self.priceLabel.hidden = NO;
        [self.priceLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.top.right.equalTo(self);
            make.width.mas_equalTo(self.layout.priceLabelWidth);
        }];
        
        [self.titleLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.left.top.equalTo(self);
            make.height.mas_equalTo(self.layout.titleLabelHeight);
            make.width.mas_equalTo(self.layout.data.widgetWidth - self.layout.priceLabelWidth - 5);
        }];
    }
    
    [self.contentLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(self);
        make.top.equalTo(self.titleLabel.mas_bottom).offset(labelPadding);
        make.height.mas_equalTo(_layout.contentLabelHeight);
    }];
    
    [self.imageContentView mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(self);
        make.top.equalTo(self.contentLabel.mas_bottom).offset(picTopPadding);
        make.height.mas_equalTo(_layout.imageContentViewHeight);
    }];
    
    self.titleLabel.textAlignment = NSTextAlignmentLeft;
    self.titleLabel.textVerticalAlignment = SEGTextVerticalAlignmentCenter;
    
    self.contentLabel.textAlignment = NSTextAlignmentCenter;
    self.contentLabel.textVerticalAlignment = SEGTextVerticalAlignmentCenter;
    
//    self.priceLabel.backgroundColor = kColor_T;
    self.priceLabel.textAlignment = NSTextAlignmentRight;
    self.priceLabel.textVerticalAlignment = SEGTextVerticalAlignmentCenter;
    
    [self seg_setImagesWithPicArray:_layout.data.imageContent];
}


/// 设置评论图片
- (void)seg_setImagesWithPicArray:(NSArray *)picArray
{
    
    [self.imageContentView removeAllSubviews];
    
    CGFloat imageHeight = picWidth * kScaleWidth;
    CGFloat padding = picPadding;

    NSUInteger countInRow;//一行最大图片个数
    if (picArray.count == 1) {
        countInRow = 1;
        imageHeight = 170  * kScaleWidth;
    }else if (picArray.count == 4) {
        countInRow = 2;
    }else {
        countInRow = 3;
    }
    [picArray enumerateObjectsUsingBlock:^(NSString *imageString, NSUInteger idx, BOOL * _Nonnull stop) {

        NSURL *url = [NSURL URLWithString:imageString];
        UIImageView *imageView = [[UIImageView alloc] init];
        imageView.contentMode = UIViewContentModeScaleAspectFill;
        imageView.layer.masksToBounds = YES;
        [imageView setImageWithURL:url placeholderImage:[UIImage imageWithContentsOfFile:SEGPicName(@"pic_default_300_300")]];
        imageView.tag = 10 + idx;
        imageView.userInteractionEnabled = YES;
        __weak typeof(self) _self = self;
        UITapGestureRecognizer *tap2 = [[UITapGestureRecognizer alloc]initWithActionBlock:^(UITapGestureRecognizer*  _Nonnull sender) {
//                    [SEGAlertView showTmepMessageWithMessage:@"imageContentView picView"];
            if (_self.delegate && [_self.delegate respondsToSelector:@selector(marketDetailViewImageTapWithImageUrlArray:SelectedImageView:SelectedIndex:)]) {
                [_self.delegate marketDetailViewImageTapWithImageUrlArray:_layout.data.imageContent SelectedImageView:(UIImageView *)sender.view SelectedIndex:sender.view.tag - 10];
            }
        }];
        [imageView addGestureRecognizer:tap2];
        
//        UIButton *imageButton = [[UIButton alloc] init];
//        imageButton.tag = 100 + idx;
//        [imageButton addTarget:self action:@selector(seg_imageButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
//        [imageButton addSubview:imageView];
        [self.imageContentView addSubview:imageView];
        
        
        CGFloat leftPadding = (idx % countInRow) * (imageHeight + padding);
        CGFloat topPadding = (idx / countInRow) * (imageHeight + padding);
        [imageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.width.height.mas_equalTo(imageHeight);
            make.top.equalTo(self.imageContentView).offset(topPadding).priorityHigh();
            make.left.equalTo(self.imageContentView).offset(leftPadding);
        }];
//        [imageView mas_makeConstraints:^(MASConstraintMaker *make) {
//            make.edges.equalTo(imageButton);
//        }];
        //        if (idx == picArray.count - 1) {
        //            [self.totalImageView mas_remakeConstraints:^(MASConstraintMaker* make) {
        //                make.left.equalTo(self.contentLabel);
        //                make.top.equalTo(imageButton.mas_bottom).offset(10).priorityHigh();
        //            }];
        //        }
    }];
}

#pragma mark - getter
-(SEGLabel *)titleLabel{
    if (!_titleLabel) {
        _titleLabel = [[SEGLabel alloc]init];
        _titleLabel.userInteractionEnabled = NO;
        _titleLabel.textAlignment = NSTextAlignmentLeft;
        _titleLabel.textVerticalAlignment = SEGTextVerticalAlignmentCenter;
        _titleLabel.displaysAsynchronously = YES;
//        _titleLabel.ignoreCommonProperties = YES;//YES会影响系统属性
        _titleLabel.fadeOnHighlight = NO;
        _titleLabel.numberOfLines = 1;
        _titleLabel.fadeOnAsynchronouslyDisplay = NO;
        _titleLabel.fadeOnAsynchronouslyDisplay = NO;
        _titleLabel.lineBreakMode = NSLineBreakByTruncatingTail;
//        _titleLabel.backgroundColor = kSegRandomColor;
//        __weak typeof(self) _self = self;
//        _titleLabel.textTapAction = ^(UIView * _Nonnull containerView, NSAttributedString * _Nonnull text, NSRange range, CGRect rect) {
//        [SEGAlertView showTmepMessageWithMessage:@"_titleLabelClick"];
//        if (_self.delegate && [_self.delegate respondsToSelector:@selector(SEGprotocoltitleLabel:)]) {
//        [_self.delegate SEGprotocoltitleLabel:_self.titleLabel];
//        }
//        };
    }
    return _titleLabel;
}

-(SEGLabel *)contentLabel{
    if (!_contentLabel) {
        _contentLabel = [[SEGLabel alloc]init];
        _contentLabel.userInteractionEnabled = NO;
        _contentLabel.textAlignment = NSTextAlignmentCenter;
        _contentLabel.textVerticalAlignment = SEGTextVerticalAlignmentCenter;
        _contentLabel.displaysAsynchronously = YES;
        _contentLabel.ignoreCommonProperties = YES;//YES会影响系统属性
        _contentLabel.fadeOnHighlight = NO;
        _contentLabel.numberOfLines = 0;
        _contentLabel.fadeOnAsynchronouslyDisplay = NO;
        _contentLabel.fadeOnAsynchronouslyDisplay = NO;
        _contentLabel.lineBreakMode = NSLineBreakByTruncatingTail;
//        _contentLabel.backgroundColor = kSegRandomColor;
//        __weak typeof(self) _self = self;
//        _contentLabel.textTapAction = ^(UIView * _Nonnull containerView, NSAttributedString * _Nonnull text, NSRange range, CGRect rect) {
//        [SEGAlertView showTmepMessageWithMessage:@"_contentLabelClick"];
//        if (_self.delegate && [_self.delegate respondsToSelector:@selector(SEGprotocolcontentLabel:)]) {
//        [_self.delegate SEGprotocolcontentLabel:_self.contentLabel];
//        }
//        };
    }
    return _contentLabel;
}

-(SEGLabel *)priceLabel{
    if (!_priceLabel) {
        _priceLabel = [[SEGLabel alloc]init];
        _priceLabel.userInteractionEnabled = NO;
        _priceLabel.textAlignment = NSTextAlignmentRight;
        _priceLabel.textVerticalAlignment = SEGTextVerticalAlignmentCenter;
        _priceLabel.displaysAsynchronously = YES;
        _priceLabel.ignoreCommonProperties = YES;//YES会影响系统属性
        _priceLabel.fadeOnHighlight = NO;
        _priceLabel.numberOfLines = 1;
        _priceLabel.fadeOnAsynchronouslyDisplay = NO;
        _priceLabel.fadeOnAsynchronouslyDisplay = NO;
        _priceLabel.lineBreakMode = NSLineBreakByTruncatingTail;
//        _priceLabel.backgroundColor = kSegRandomColor;
//        __weak typeof(self) _self = self;
//        _priceLabel.textTapAction = ^(UIView * _Nonnull containerView, NSAttributedString * _Nonnull text, NSRange range, CGRect rect) {
//        [SEGAlertView showTmepMessageWithMessage:@"_priceLabelClick"];
//        if (_self.delegate && [_self.delegate respondsToSelector:@selector(SEGprotocolpriceLabel:)]) {
//        [_self.delegate SEGprotocolpriceLabel:_self.priceLabel];
//        }
//        };
    }
    return _priceLabel;
}


-(UIView *)imageContentView{
    if (!_imageContentView){
        _imageContentView = [[UIView alloc]init];
        _imageContentView.userInteractionEnabled = YES;
//        _imageContentView.backgroundColor = kColor_bg;
        //_imageContentView.layer.cornerRadius = 2;
        //_imageContentView.layer.masksToBounds = YES;
        //_imageContentView.layer.borderColor = kColor_L2.CGColor;
        //_imageContentView.layer.borderWidth = 0.5;
//        _imageContentView.backgroundColor = kSegRandomColor;
    }
    return _imageContentView;
}


@end
